import { _decorator, Component, director, EditBox, instantiate, Label, log, Node, Prefab, Scene } from 'cc';
import { NetworkManager } from '../Global/NetworkManager';
import { ApiMsgEnum, IApiPlayerListRes, IApiRoomListReq, IApiRoomListRes, IMsgGameStart, IMsgRoom } from '../Common';
import DataManager from '../Global/DataManager';
import { SceneEnum } from '../Enum';
import { PlayerManager } from '../UI/PlayerManager';
import { deepClone } from '../Utils';
const { ccclass, property } = _decorator;
@ccclass('RoomManager')
export class RoomManager extends Component {
    @property(Node)
    playerContainer: Node;

    @property(Prefab)
    playerPrefab: Prefab;

    @property(Label)
    buttonGameSetup:Label;//准备按键

    playerSetUp:number;//玩家准备
    playerList:PlayerManager[]=[];

    protected onLoad(): void {
        NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoom, this.renderPlayer, this);
        NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgGameStart, this.handleGameStart, this);
    }
    protected start(): void {

        // this.playerContainer.destroyAllChildren();
        director.preloadScene(SceneEnum.Battle);//  预加载场景
        director.preloadScene(SceneEnum.Hall);// 预加载场景

        this.renderPlayer({
            room: DataManager.Instance.roomInfo,
        })
    }

    protected onDestroy(): void {
        NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgRoom, this.renderPlayer, this);
        NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgGameStart, this.handleGameStart, this);
    }

    renderPlayer({ room: { players: list } }: IMsgRoom) {
        for (const c of this.playerContainer.children) {
            c.active = false;
        }

        while (this.playerContainer.children.length < list.length) {
            const node = instantiate(this.playerPrefab)
            node.active = false;
            node.setParent(this.playerContainer);
        }
        // console.log("list", list);
        for (let i = 0; i < list.length; i++) {
            const data = list[i];
            const node = this.playerContainer.children[i];
            console.log("当前房间", data);
            node.getComponent(PlayerManager).init(data)
        }
    }
   
    async handleLeaveRoom() {
        const { success, error, res } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomLeave, {
        })
        if (!success) {
            console.error("连接失败" + error);
            return;
        }

        DataManager.Instance.roomInfo = null;
        director.loadScene(SceneEnum.Hall);//跳转场景
    }

    // 开始游戏
    async hanleStart(){
        // if()

        const { success, error, res } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiGameStart, {
        })
        if (!success) {
            console.error("连接失败" + error);
            return;
        }
    }

    handleGameStart({state}:IMsgGameStart){
        DataManager.Instance.state=state;
        DataManager.Instance.lastState=deepClone(state);// 保存上一次状态
        director.loadScene(SceneEnum.Battle);// 跳转场景
    }
}

